<template>
  <div class="boxContent">
    <div class="appBox">
      <div class="searchList">
        <el-form :model="state.params"
                 ref="FormInstance "
                 class="boxFrom">
          <div class="searchBox">
            <template v-for="(item, index) in state.totalInputColumns"
                      :key="index">
              <el-form-item v-if="item.type == 'input'">
                <el-input clearable
                          v-model="state.params[item.key]"
                          :placeholder="item.label"></el-input>
              </el-form-item>
              <el-form-item v-if="item.type == 'select'">
                <el-select v-model="state.params[item.key]"
                           :placeholder="item.label"
                           clearable>
                  <el-option label="正常"
                             value="0" />
                  <el-option label="停用"
                             value="1" />
                  <el-option label="即将欠费"
                             value="2" />
                  <el-option label="欠费"
                             value="3" />
                </el-select>
              </el-form-item>
              <el-form-item v-if="item.type == 'time'">
                <el-date-picker v-model="state.timexx"
                                type="daterange"
                                range-separator="至"
                                start-placeholder="开始时间"
                                value-format="YYYY-MM-DD"
                                end-placeholder="结束时间" />
              </el-form-item>

            </template>
          </div>

          <el-form-item class="btns">
            <!-- <el-button v-preventReClick type="primary"
                       @click="methods.handleAction('export')"
                       v-permission="15">下载账单</el-button> -->
            <el-button v-preventReClick
                       type="primary"
                       @click="methods.handleAction('plck')"
                       v-permission="1140">批量催款</el-button>
            <el-button v-preventReClick
                       type="primary"
                       plain
                       @click="methods.handleAction('query')">搜索</el-button>
            <el-button v-preventReClick
                       @click="methods.handleAction('reset')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="tabaleBox">
        <el-table border
                  v-loading="state.loading"
                  :data="state.tableData"
                  style="width: 100%"
                  @selection-change="methods.handleSelectionChange"
                  :header-cell-style="{ color: '#000' }">
          <el-table-column type="selection"
                           width="50"
                           :selectable="methods.selectable" />
          <el-table-column prop="name"
                           label="企业名称"
                           show-overflow-tooltip
                           align="left" />
          <el-table-column prop="money"
                           label="企业当前余额"
                           show-overflow-tooltip
                           align="center"
                           width="120" />
          <el-table-column prop="yesterdayamount"
                           label="昨日扣款"
                           show-overflow-tooltip
                           align="center"
                           width="120" />
          <el-table-column prop="signamount"
                           label="打卡扣款"
                           show-overflow-tooltip
                           align="center"
                           width="120" />
          <el-table-column prop="inducationamount"
                           label="入职扣款"
                           show-overflow-tooltip
                           width="150"
                           align="center" />
          <el-table-column prop="avgamount"
                           label="七日平均消耗 "
                           show-overflow-tooltip
                           width="150"
                           align="center" />
          <el-table-column prop="status"
                           label="状态"
                           show-overflow-tooltip
                           align="center"
                           width="100">
            <template #default="scope">
              <div v-if="scope.row.status == 0"
                   class="primary">正常</div>
              <div v-if="scope.row.status == 1"
                   class="info">停用</div>
              <div v-if="scope.row.status == 2"
                   class="warning">即将欠费</div>
              <div v-if="scope.row.status == 3"
                   class="error">欠费</div>
            </template>
          </el-table-column>
          <el-table-column prop="status"
                           label="查看账单"
                           show-overflow-tooltip
                           align="center"
                           width="100">
            <template #default="scope">
              <div class="primary cup"
                   v-permission="1142"
                   @click="$router.push('/balance/enterpriseBill?id='+scope.row.cid)">查看账单</div>
            </template>
          </el-table-column>
          <el-table-column prop="btns"
                           label="操作"
                           show-overflow-tooltip
                           align="center"
                           width="80"
                           fixed="right">
            <template #default="scope">
              <div class=" btnsDatil">
                <div v-if="scope.row.status == '2' || scope.row.status == '3'"
                     v-permission="1141"
                     @click="methods.handleAction('ck', scope.row)">催款</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <el-pagination background
                       v-model:current-page="state.params.pageNum"
                       v-model:page-size="state.params.pageSize"
                       :page-sizes="[10, 20, 30, 50]"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="state.totalSize"
                       @size-change="methods.handleSizeChange"
                       @current-change="methods.handleCurrentChange" />
      </div>
    </div>

  </div>
</template>

<script setup>
import { onMounted, reactive, getCurrentInstance } from "vue";
import { ElMessage, ElLoading, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
const app = getCurrentInstance()?.proxy;
const initState = () => {
  return {
    loading: false,
    dialogVisible: false,
    totalSize: 0,
    timexx: "",
    tableData: [],
    params: {
      pageNum: 1,
      pageSize: 10,
      cid: JSON.parse(localStorage.getItem('userInfor')).cid,
      account: JSON.parse(localStorage.getItem('userInfor')).account,
    },
    totalInputColumns: [
      // { key: "startTime", label: "开始日期", type: "time" },
      { key: "name", label: "企业名称", type: "input" },
      { key: "status", label: "选择状态", type: "select" },
    ],
    tableIdsSelect: [],
  };
};
const state = reactive(initState());
onMounted(() => {
  state.optionsPre = JSON.parse(localStorage.getItem("profession"));
  methods.getDataList();

});
const methods = {

  // 禁用表格选择
  selectable (row, index) {
    if (row.status == '0' || row.status == '1') {
      return false;
    } else {
      return true;
    }
  },

  // 表格多选
  handleSelectionChange (e) {
    console.log(e, "表格多选");
    state.tableIdsSelect = e.map((res) => res.cid);
    console.log(state.tableIdsSelect);
  },

  handleAction (type, data) {
    switch (type) {

      case "plck": //查询
        if (state.tableIdsSelect.length - 0 == 0) {
          ElMessage.error('请最少选择一条数据催款')
        } else {
          methods.getck({ cid: state.tableIdsSelect })
        }
        break;
      case "ck": //查询
        methods.getck({ cid: [data.cid] })
        break;
      case "query": //查询
        methods.getDataList();
        break;
      case "reset": // 重置
        state.params = initState().params;
        break;
      case "edit": // 编辑
        router.push("/balance/reconciliationDetails?id=" + data.id);
        break;
    }
  },

  getck (data) {
    app.$post('/companywalletflow/sendNote', data).then(res => {
      console.log(res);
      if (res.code == 200) {
        ElMessage.success('催款成功')
      }

    })
  },
  getDataList () {
    if (state.timexx?.length) {
      state.params.startTime = state.timexx[0];
      state.params.endTime = state.timexx[1];
    } else {
      state.params.startTime = ''
      state.params.endTime = ''
    }
    state.loading = true
    app.$get("/companywalletflow/queryCompanyAmount", state.params).then((res) => {

      state.tableData = res.data.data;
      state.totalSize = res.data.total;
      state.loading = false
    }).catch(() => {
      state.loading = false
    })
  },
  //分页变化
  handleSizeChange () {
    state.params.pageNum = 1;
    methods.getDataList();
  },
  //表格翻页
  handleCurrentChange () {
    methods.getDataList();
  },
};
</script>

<style lang="scss" scoped>
.appBox {
  background: #fff;
  border-radius: 5px;
  padding: 20px;
  height: calc(100% - 20px);
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .tabaleBox {
    overflow: hidden;
    flex: 1;
  }
  .btnsDatil {
    color: #0079fe;
    cursor: pointer;
    align-items: center;
  }
  .pagination {
    margin-top: 20px;
    height: 50px;
    display: flex;
    justify-content: flex-end;
  }
  ::v-deep(.el-dialog__body) {
    max-height: 380px;
  }
  .imgs {
    flex-wrap: wrap;
    .forbox {
      height: 150px;
      margin: 10px 0 0 10px;
    }
    .imgBox {
      height: 100%;
      width: auto;
    }
    .videoBox {
      width: auto;
      height: 100%;
    }
  }
}
</style>
